package com.it.springbootspringdatajpa.controller;

import com.it.springbootspringdatajpa.domain.Contract;
import com.it.springbootspringdatajpa.service.IContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @projectName: springboot-spring-data-jpa
 * @package: com.it.springbootspringdatajpa.controller
 * @className: ContractController
 * @author: Navid
 * @description: TODO
 * @date: 2021/10/23 19:42
 * @version: 1.0
 */
@RestController
public class ContractController {

    @Autowired
    IContractService service;

    @GetMapping("/insert")
    public void insert() {
        Contract contract = new Contract();
        contract.setContractNo("100001");
        contract.setContactName("xiao qiang");
        contract.setTelephone("1234567890");
        contract.setAmount(10000.3);
        contract.setTax(123.3);
        contract.setIncludedTax(true);
        contract.setContractStatus(true);
        contract.setContractType("临时工");
        contract.setBeginTime(new Date(System.currentTimeMillis()));
        contract.setEndTime(new Date(System.currentTimeMillis() + 1000000));
        service.insert(contract);
    }

    @GetMapping("/updateById")
    public void updateById(int id) {
        System.out.println("update id:" + id);
        service.updateNameById("UP UUP", id);
    }

    @GetMapping("/deleteById")
    public void deleteById(int id) {
        System.out.println("delete id:" + id);
        service.deleteById(id);
    }

    @GetMapping("/findAll")
    public Map<String, Object> findAll() {
        Map<String, Object> map = new HashMap<>(10);
        List<Contract> list = service.findAll();
        map.put("data", list);
        return map;
    }

    @GetMapping("/findByCondition")
    public Map<String, Object> findByCondition(String contractNo, String contractType, String contactName) {
        Map<String, Object> map = new HashMap<>(10);
        Contract contract = service.findByCondition(contractNo, contractType, contactName);
        map.put("data", contract);
        return map;
    }

    @GetMapping("/findByStatusAndDate")
    public Map<String, Object> findByStatusAndDate(Boolean isContractSigned) {
        Map<String, Object> map = new HashMap<>(10);
        List<Contract> list = service.findByContractStatus(isContractSigned);
        map.put("data", list);
        return map;
    }

    @GetMapping("/findByPaged")
    public Map<String, Object> findByPaged(Integer pageIndex, Integer pageSize, String contractNo) {
        Map<String, Object> map = new HashMap<>(10);
        List<Contract> list = service.findByPaged(pageIndex, pageSize, contractNo);
        map.put("data", list);
        return map;
    }

}
